function $key(getkeys) {
    return document.querySelector(getkeys);
}


var top = document.getElementsByClassName("top")[0];
var nav = document.getElementsByClassName("nav")[0];
// 提取日期相关函数
date();
function date() {
    var spa = document.querySelector(".dater");
    var timer = null;
    // 封装时间日期格式化
    function mydate() {
        var date = new Date();
        // 时间日期格式化 yyyy-mm-dd hh:mm:ss
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var data = date.getDate();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var seconds = date.getSeconds();
        // 补 0
        month = (month < 10) ? "0" + month : month;
        data = (data < 10) ? "0" + data : data;
        hours = (hours < 10) ? "0" + hours : hours;
        minutes = (minutes < 10) ? "0" + minutes : minutes;
        seconds = (seconds < 10) ? "0" + seconds : seconds;
        var res = `${year}年${month}月${data}日 ${hours}:${minutes}:${seconds}`;
        return res;
    }

    function automove() {
        spa.innerHTML = mydate();
    }
    // 自动走
    setInterval(automove, 1000);
}


var username = sessionStorage.getItem('userAccount');
document.querySelector('.namer').textContent = username;

// var projectName = localStorage.getItem("projectName");
var projectId = localStorage.getItem("projectId");

var Wpm = document.querySelector(".wpm");
var Img = document.querySelector(".Img");
Img.addEventListener("mouseover",function(){
    Wpm.style.display = "block";
})
Img.addEventListener("mouseout",function(){
    Wpm.style.display = "none";
})

Wpm.addEventListener("mouseover",function(){
    Wpm.style.display = "block";
})
Wpm.addEventListener("mouseout",function(){
    Wpm.style.display = "none";
})
// 添加子项目
var btn1 = document.querySelector(".btn1");
btn1.onclick = function(){
    var Masks = document.createElement("div");
        Masks.className = "Masks";
        $key(".nav_content").insertBefore(Masks, $key(".nav_content").firstElementChild);
        var Modal = document.createElement("div");
        Modal.className = "Modal";
        Modal.innerHTML = `
            <div class="wtp">添加子项目</div>
            <div class="wto">
                <div>项目名称：<input class="cst1" type="text"></div>
                <div>负责人<select class="charg"></select></div>
                <div>项目工期：<input  class="cst2" type="text"></div>
               <div>开始时间： <input class="cst3" type="date"></div>
               <div>结束时间 <input class="cst4" type="date"></div>
               <div>比重 <input class="cst5" type="text"></div>
               
            </div>
            <div class="wtf">
                <button class="Confir">确认</button>
                <button class="Cance">取消</button>
            </div>
        ` 
        $key(".nav_content").insertBefore(Modal, $key(".nav_content").firstElementChild.nextSibling);
        charge();
        Confirm();
        Cancel();
}

//项目负责人
function charge(){
    var charg = document.querySelector(".charg");
   ajax.post("http://192.168.199.101:8080/BaorSystem/projector/addProjectUserList",{}, function (res) {
       if (res.code == 200) {
         var dataset = res.dataset;
           for(var i=0;i<dataset.length;i++){
               var newoption = document.createElement("option");
               newoption.innerHTML = `${dataset[i].userName}`;
               newoption.value = `${dataset[i].userId}`
               charg.appendChild(newoption);
           }
           
       }
   })
}

//添加项目 - 确认
function Confirm(){
    var Masks = document.querySelector(".Masks");
    var Modal = document.querySelector(".Modal");
    var Confir = document.getElementsByClassName("Confir")[0];
    Confir.onclick = function(){
        var cst1 = document.getElementsByClassName("cst1")[0];
        var cst2 = document.getElementsByClassName("cst2")[0];
        var cst3 = document.getElementsByClassName("cst3")[0];
        var cst4 = document.getElementsByClassName("cst4")[0];
        var cst5 = document.getElementsByClassName("cst5")[0];
        var charg = document.querySelector(".charg");
        var slt = {
            childProjectName:cst1.value,
            childProjectLeader:Number(charg.value),
            workingHours:cst2.value,
            startDate:cst3.value,
            endDate:cst4.value,
            prop:cst5.value,
            projectId:Number(projectId),
        }
        ajax.post("http://192.168.199.101:8080/BaorSystem/child/insertChild",slt,function(res){
            console.log(res);
            
        })
        $key(".nav_content").removeChild(Masks);
        $key(".nav_content").removeChild(Modal);
        mov();
    }
}

//添加项目 - 取消
function Cancel(){
    var Cance = document.getElementsByClassName("Cance")[0];
    var Masks = document.querySelector(".Masks");
    var Modal = document.querySelector(".Modal");
    Cance.addEventListener("click",function(){
        $key(".nav_content").removeChild(Masks);
        $key(".nav_content").removeChild(Modal);
    })
}

//添加项目组成员
var btn2 = document.querySelector(".btn2");
btn2.onclick = function(){
    var Masks = document.createElement("div");
        Masks.className = "Masks";
        $key(".nav_content").insertBefore(Masks, $key(".nav_content").firstElementChild);
        var Modal = document.createElement("div");
        Modal.className = "Modal";
        Modal.innerHTML = `
            <div class="wtp">添加任务</div>
            <div class="wto">
                <div>负责人<select class="charg"></select></div>
            </div>
            <div class="wtf">
                <button class="Confira">确认</button>
                <button class="Cance">取消</button>
            </div>
        ` 
        $key(".nav_content").insertBefore(Modal, $key(".nav_content").firstElementChild.nextSibling);
        charge();
        Confirma();
        Cancel();
}

//添加成员 - 确认
function Confirma(){
    var Masks = document.querySelector(".Masks");
    var Modal = document.querySelector(".Modal");
    var Confira = document.getElementsByClassName("Confira")[0];
    Confira.onclick = function(){
        var charg = document.querySelector(".charg");
        console.log(charg.value);
        console.log(projectId);
        var DATA = [
            {
                "userId":Number(charg.value),
                "projectId":Number(projectId),
            }
        ]
            
        
        ajax.post("http://192.168.199.101:8080/BaorSystem/projector/insertProjector",DATA,function(res){
            
        })
        $key(".nav_content").removeChild(Masks);
        $key(".nav_content").removeChild(Modal);
        ovo();
    }
}

var pageIndex = 1;
var pageSize = 5;
var Tbody = document.querySelector(".Tbody");
var Txl = document.querySelector(".Txl");
//项目组成员 
function ovo() {
    Tbody.innerHTML = "";
    var data = {
        // pageIndex: pageIndex,
        // pageSize: pageSize,
        projectId: projectId,
        // projectChildName:projectName,
    }
    ajax.post("http://192.168.199.101:8080/BaorSystem/projector/projectorList", data, function (res) {
        if (res.code == 200) {
            console.log(res);
            dataset = res.dataset;
            for (var i in dataset) {
                var newTr = document.createElement("tr");
                newTr.innerHTML = `
                   <td class="username">${dataset[i].userId}</td>
                   <td class="userphone">${dataset[i].userPhone}</td>
                   <td class="sta">${dataset[i].status}</td>
               `;
                Txl.appendChild(newTr);
                sx();
            }

        }
    })
}
ovo();


// 项目列表
function mov() {
    Tbody.innerHTML = "";
    var data = {
        pageIndex: pageIndex,
        pageSize: pageSize,
        projectId: projectId,
        // projectChildName:projectName,
    }
    ajax.post("http://192.168.199.101:8080/BaorSystem/child/childList", data, function (res) {
        if (res.code == 200) {
            console.log(res);
            childList = res.dataset.childList;
            for (var i in childList) {
                var newTr = document.createElement("tr");
                newTr.innerHTML = `
                     <td>${childList[i].childProjectId}</td>
                     <td>${childList[i].childProjectName}</td>
                     <td>${childList[i].childProjectLeader}</td>
                     <td>${childList[i].workingHours}</td>
                     <td>${childList[i].startDate}</td>
                     <td>${childList[i].endDate}</td>
                     <td>${childList[i].progress}</td>
                     <td><button class="detai">详情</button></td>
                 `;
                Tbody.appendChild(newTr);
            }
            pageCount = res.dataset.pageCount;
            var fot = document.querySelector(".fot");
            fot.innerHTML = "";
            for (var i = 1; i <= pageCount; i++) {
                var But = document.createElement("button");
                But.innerText = i;
                But.className = "page";
                fot.appendChild(But);
                Page();
            }
            detail();
        }
    })

}
mov();

function sx(){
    var sta = document.querySelectorAll(".sta");
    for(var i=0;i<sta.length;i++){
        if(sta[i].innerHTML == "空闲"){
            sta[i].style.color = "green"; 
        }else{
            sta[i].style.color = "red";
        }
    }
}
// 总班级上一页
var first = document.querySelector(".first");
var last = document.querySelector(".last");
first.onclick = function () {
    pageIndex--;
    if (pageIndex <= 1) {
        pageIndex = 1;
        this.disabled = true;
    }
    last.disabled = false;
    mov();
}


// 总班级下一页
last.onclick = function () {
    pageIndex++;
    if (pageIndex >= pageCount) {
        pageIndex = pageCount;
        this.disabled = true;
    }
    first.disabled = false;
    mov();
    // sou();
}


//页码点击
function Page() {
    var page = document.getElementsByClassName("page");
    for (var j = 0; j < page.length; j++) {
        page[j].onclick = function () {
            for (var i = 0; i < page.length; i++) {
                if (this == page[i]) {
                    pageIndex = i + 1;
                    console.log(page[i]);
                    page[i].style.backgroundColor = "blue";
                    page[i].style.color = "#fff";
                    mov();
                } else {
                    page[i].style.backgroundColor = "#fff";
                    page[i].style.color = "#000";
                }
            }

        }
    }
}


//详情
function detail() {
    var detai = document.getElementsByClassName("detai");
    for (var i = 0; i < detai.length; i++) {
        detai[i].onclick = function () {
            for (var j = 0; j < detai.length; j++) {
                if (this == detai[j]) {
                    var childProjectId = childList[j].childProjectId;
                    localStorage.setItem("zzz", childProjectId)
                    location.href = "Subproject.html";
                }
            }
        }
    }
}


// 添加点击事件
//日志系统
var journal = document.getElementsByClassName("journal")[0];
journal.onclick = function () {
    location.href = "./diary.html";
}
//首页
var classs = document.getElementsByClassName("classs");
classs[0].onclick = function () {
    location.href = "./header.html";
}
//日常
var routine = document.getElementsByClassName("routine")[0];
routine.onclick = function () {
    location.href = "./vacate.html";
}
//系统设置
var system = document.getElementsByClassName("system")[0];
system.onclick = function () {
    location.href = "./setting.html";
}
//项目管理
var project = document.getElementsByClassName("project")[0];
project.onclick = function () {
    location.href = "./projectmanagement.html";
}
//请假记录
var excused = document.querySelector(".excused");
excused.addEventListener('click', function () {
    location.href = "./vacate.html";
});
//考勤记录
var presence = document.querySelector(".presence");
presence.addEventListener('click', function () {
    location.href = "./clock.html";
});